package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.dao.TbContentCategoryMapper;
import com.kgc.entity.TbContentCategory;
import com.kgc.entity.TbContentCategoryExample;
import com.kgc.service.TbCatService;
import com.kgc.utils.ItripResult;
import com.kgc.utils.ResultTable;
import com.kgc.utils.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class TbCatServiceImpl implements TbCatService {
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;
    @Override
    public ResultTable findAll(int page, int pageSize) {
        ResultTable resultTable = new ResultTable();
        PageHelper.startPage(page,pageSize);
        TbContentCategoryExample example = new TbContentCategoryExample();
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);
        PageInfo<TbContentCategory> pageInfo = new PageInfo<>(list);
        resultTable.setCount(pageInfo.getTotal());
        resultTable.setCode(0);
        resultTable.setData(pageInfo.getList());
        resultTable.setMsg("ojbk");
        return resultTable;
    }

    @Override
    public List<TreeNode> findTree() {
        List<TreeNode> tree = tbContentCategoryMapper.findTree();
        return tree;
    }

    @Override
    public void deleteById(Long id) {
        tbContentCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ItripResult addContentCategory(Long pid, String name) {
        TbContentCategory contentCategory=new TbContentCategory();
        contentCategory.setName(name);
        contentCategory.setParentId(pid);
        contentCategory.setSortOrder(1);
        contentCategory.setStatus(1);
        contentCategory.setIsParent(false);
        contentCategory.setCreated(new Date());
        contentCategory.setUpdated(new Date());
        tbContentCategoryMapper.insertSelective(contentCategory);
        return ItripResult.ok();//status 200 msg ok  datanull
    }
    @Override
    public ItripResult updateContentCategory(Long id, String name) {
        TbContentCategory contentCategory=new TbContentCategory();
        contentCategory.setId(id);
        contentCategory.setName(name);
        contentCategory.setUpdated(new Date());
        tbContentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
        return ItripResult.build(200,"修改成功!");
    }
}
